#!/bin/bash

# define all constant
letters=(a b c d e f g h i j)
ship="x"
ocean="."

getF(){	echo ${field[$(($1*10+$2))]};}
setF(){ field[$(($1*10+$2))]="$3";}
fillF(){
	for ((i=0;i<100;i++));do
		field[$i]=$ocean
	done
}
set2Numeric(){
	for ((i=0;i<10;i++));do
	if [ ${letters[$i]} == $1 ];then
		echo $i
	fi
	done
}
printF()
{
	echo " " {0..9}
	for ((i=0;i<10;i++));do
		echo -n "${letters[$i]} "
		for ((j=0;j<10;j++));do
			echo -n `getF ${i} ${j}`" "
		done
		echo ""
	done
}
if [ $# -eq 1 ] && [ $1 == "--help" ] ; then
  echo \
"This program draw field from battleship game and
place ships according to the arguments.
Number of arguments must be even!
For example
  ./script.sh a2 a4
  ./script.sh a2 a4 b7 b4
As a result you\`ll get image of field with your ships"
  exit
fi
if [ $#%2 == 1 ]; then
  echo Wrong number of arguments!
  echo Pls use --help for description
  exit
fi
# fill array
fillF
# while new arguments exist
while [ $# -ge 2 ];do
	# parse agrs
	x1=${1:0:1} #letter
	y1=${1:1:1} #numer
	x2=${2:0:1} #letter
	y2=${2:1:1} #numer
	# Change letters to equal number
	x1=`set2Numeric $x1`
	x2=`set2Numeric $x2`
	# if ship has horizontal position
	if [ $x1 -eq $x2 ]; then
		# call setF for every coordinates
		for y in `eval echo {$y1..$y2} | sed -e "s/ /\n/g"`; do
			setF $x1 y $ship
		done
	# if ship has vertical position
	elif [ $y1 -eq $y2 ]; then
		# call setF for every coordinates
		for x in `eval echo {$x1..$x2} | sed -e "s/ /\n/g"`; do
			setF x $y1 $ship
		done
	else
	# if position is wrong 
		echo "Wrong placement $1 $2"
	fi
	# shift agruments by 2 position
	shift 2
done
printF

# This script is just working. There are no any complicated checking rules.